/*
* Copyright (C) 2024-2025 Uniontech Technology Co., Ltd.
*
* Author:      zhangbingyi <zhangbingyi@uniontech.com>
*
* Maintainer:  zhangbingyi <zhangbingyi@uniontech.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "waylandwarningdialog.h"

#include <QLabel>

//#include <QScroller>
#include <QPushButton>
#include <QScrollBar>
#include <QScrollArea>
#include <QBoxLayout>

WaylandWarningDialog::WaylandWarningDialog(QWidget *parent)
    : DDialog(parent)
{
    setAccessibleName("waylandWarningDialog");

    init();
}

WaylandWarningDialog::~WaylandWarningDialog()
{
}

void WaylandWarningDialog::init()
{
    Qt::WindowFlags flags = windowFlags();
    flags |= Qt::WindowStaysOnTopHint;
    setWindowFlags(flags);
//    setWindowFlag(Qt::WindowStaysOnTopHint);
    setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    setModal(true);
    setAttribute(Qt::WidgetAttribute::WA_DeleteOnClose);
    setIcon(QIcon::fromTheme(DIALOG_TIP_YELLOW));

    // 内容控件
    QWidget *contentWidget = new QWidget(this);
    QVBoxLayout *contentLayout = new QVBoxLayout;
    contentWidget->setLayout(contentLayout);
    contentWidget->setAccessibleName("waylandWarningDialog_contentWidget");
    contentLayout->setContentsMargins(0, 0, 0, 0);
    contentLayout->setSpacing(0);

    // 设置标题
    QLabel *title = new QLabel(contentWidget);
    title->setAccessibleName("contentWidget_title");
    title->setContentsMargins(20, 0, 0, 0);
    title->setText(tr("Set up security levels of your system"));
    QFont ft = title->font();
    ft.setBold(true);
    title->setFont(ft);
    contentLayout->addWidget(title, 0, Qt::AlignCenter);
    contentLayout->addSpacing(5);

    // 添加滑动条区域
    QScrollArea *scrollArea = new QScrollArea(contentWidget);
    scrollArea->setAccessibleName("contentWidget_scrollArea");
    scrollArea->viewport()->setAccessibleName("scrollArea_viewWidget");
    scrollArea->setContentsMargins(0, 0, 0, 0);
    scrollArea->setFrameShape(QFrame::Shape::NoFrame);
    scrollArea->setWidgetResizable(true);
    scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarPolicy::ScrollBarAlwaysOff);

//    // 触屏移动
//    QScroller::grabGesture(scrollArea->viewport(), QScroller::LeftMouseButtonGesture);
//    QScroller *scroller = QScroller::scroller(scrollArea);
//    QScrollerProperties sp;
//    sp.setScrollMetric(QScrollerProperties::HorizontalOvershootPolicy, QScrollerProperties::OvershootAlwaysOff);
//    scroller->setScrollerProperties(sp);

    // 设置滑动条区域背景颜色为透明
    QPalette pa = scrollArea->palette();
    pa.setBrush(QPalette::Window, Qt::transparent);
    scrollArea->setPalette(pa);

    // 设置文本文案
    QLabel *txt = new QLabel("", this);
    txt->setText(tr("The Wayland mode does not support high security level,\n please switch to X11 mode and then enable it")); // 根据类型设置文本文案内容
    txt->setAccessibleName("viewWidget_tipLable");
    txt->setWordWrap(true);
    txt->setMinimumWidth(400);
    txt->setAlignment(Qt::AlignCenter);
    scrollArea->setWidget(txt); // 添加到滑动条控件中
    contentLayout->addWidget(scrollArea);
    scrollArea->setFixedHeight(txt->height()); // 滑动条区域高度由内容高度自适应

    addContent(contentWidget);

    // 针对DTK弹窗目前accessibleName会随系统语言变化的情况
    // 通过insertButton的形式定制按钮,保持accessiableName的一致性

    QPushButton *okBtn = new QPushButton(this);
    okBtn->setText(tr("Confirm", "button"));
    okBtn->setObjectName("okBtn");
    okBtn->setAccessibleName("waylandWarningDialog_okButton");
    okBtn->setAttribute(Qt::WA_NoMousePropagation);
    this->insertButton(1, okBtn, false);

    connect(this, SIGNAL(buttonClicked(int, const QString &)), this, SLOT(close()));
}

void WaylandWarningDialog::closeEvent(QCloseEvent *event)
{
    Q_UNUSED(event);
    deleteLater();
}
